package sg.edu.nus.iss.billing.service.subscription.feature;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.dao.bill.TransactionDAO;
import sg.edu.nus.iss.billing.vo.subscription.feature.FeatureVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.IDDCallFeatureVO;
import sg.edu.nus.iss.billing.vo.transaction.TransactionVO;

public class IDDCallFeature extends Feature {

	private IDDCallFeatureVO iddCallFeatureVO;
	private double usageCharges=0;
	@SuppressWarnings("deprecation")
	@Override
	public double computeCharges(FeatureVO featureVO, String subscriptionId, Date date) {
		// TODO Auto-generated method stub
		if(featureVO instanceof IDDCallFeatureVO)
		{
			iddCallFeatureVO= (IDDCallFeatureVO)featureVO;
			iddCallFeatureVO.getPerSecRate();
			TransactionDAO transactionDAO = new TransactionDAO();
			List<TransactionVO> transactionList = transactionDAO.getTransactions(subscriptionId, "IDD Call");
			for(Iterator<TransactionVO> i = transactionList.iterator(); i.hasNext(); ) 
			{
				TransactionVO transaction = i.next();
				//if first month of bill
				if(date.compareTo(Util.dateAddDays(featureVO.getStartDate(),31))>=0)
				{	
					if(Util.isBetweenDate(featureVO.getStartDate(), date, transaction.getTransactionDate()))
					{
					usageCharges+=transaction.getCallDuration() * iddCallFeatureVO.getPerSecRate();
					}
				}
				// if not first month get trans between last printed month and current date
				// assumes last printed date is -1 month of current date
				else 
				{
					if(Util.isBetweenDate(Util.dateAddMonth(date, -1), date, transaction.getTransactionDate()))
					{
					usageCharges+=transaction.getCallDuration() * iddCallFeatureVO.getPerSecRate();
					}
				}
			}
			
		}
	
		return usageCharges;
	}

}
